#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
void SeqListInit(SL* ps) {
	ps->arr = NULL;
	ps->size = ps->capecity = 0;
}
void SeqListDest(SL* ps) {
	if (ps->arr != NULL) {
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capecity = 0;
}
void SeqListExpand(SL* ps) {
	if (ps->capecity == 0) {
		ps->capecity = 2;
	}
	ps->capecity *= 2;
	SLDateType* temp = (SLDateType*)realloc(ps->arr, (ps->capecity ) * sizeof(int));
		if (temp==NULL) {
			exit(1);
		}
		ps->arr = temp;
}
void SeqListPushBack(SL* ps, SLDateType x)
{
	if (ps->size >= ps->capecity) {
		SeqListExpand(ps, 1);
	}
	assert(ps->arr);
	ps->arr[ps->size] = x;
	ps->size++;
}
void SeqListPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
}
void SeqListPushFront(SL* ps, SLDateType x) {
	if (ps->size >= ps->capecity) {
		SeqListExpand(ps, 1);
	}
	assert(ps->arr);
	for(int i=ps->size; i>0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}
void SeqListInsert(SL* ps, int pos, SLDateType x) {
	assert(pos >= 0 && pos <= ps->size);
	if (ps->size >= ps->capecity) {
		SeqListExpand(ps, 1);
	}
	assert(ps->arr);
	for (int i=ps->size; i>pos;i--) {
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}
int SeqListFind(SL* ps, SLDateType x) {
	assert(ps->arr);
	for (int i = 0; i < ps->size; i++) {
		if (ps->arr[i] == x)
			return i;
	}
	return -1;
}
void SeqListErase(SL*ps, int pos) {
	assert(ps->arr);
	assert(pos >= 0 && pos <= ps->size);
	for (int i=pos; i<ps->size-1;i++) {
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}